<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出库时间" prop="outboundTime">
        <el-date-picker clearable
                        v-model="queryParams.outboundTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择出库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="仓库编号" prop="warehouseCode">
        <el-select
          v-model="queryParams.warehouseCode"
          placeholder="请选择仓库"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="w in warehouseList"
            :key="w.id"
            :label="w.warehouseName"
            :value="w.warehouseCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关键字查询" prop="mark" label-width="90px">
        <el-input
          v-model="queryParams.mark"
          placeholder="请输入关键字查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['inventory:outbound:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['inventory:outbound:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['inventory:outbound:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="outboundList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="出库编号" align="center" prop="outboundCode" />
      <el-table-column label="出库单名称" align="center" prop="outboundName" />
      <el-table-column label="关联合同订单" width="100" align="center" prop="orderCode" />
      <el-table-column label="仓库编号" align="center" prop="warehouseCode" />
      <el-table-column label="出库数量" align="center" prop="outboundNum" />
      <el-table-column label="出库人员" align="center" prop="ename" />
      <el-table-column label="出库时间" align="center" prop="outboundTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outboundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status==1 ? 'success' : 'danger'">{{ scope.row.status==1 ? '已出库' : '未出库' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="userName" />
      <el-table-column label="备注" align="center" prop="mark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.status !== 1"
            @click="handleConfirm(scope.row)"
          >确定出库</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['inventory:outbound:remove']"
          >{{ scope.row.status === 1 ? '撤销' : '删除' }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetails(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { updateStatus, listOutbound, delOutbound, addOutbound, getWarehouseList } from "@/api/inventory/outbound";

export default {
  name: "Outbound",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 仓库集合
      warehouseList: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // #出库单表格数据
      outboundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outboundName: null,
        orderCode: null,
        warehouseCode: null,
        outboundNum: null,
        employeeId: null,
        outboundTime: null,
        status: null,
        createId: null,
        mark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        outboundName: [
          { required: true, message: "出库单名称不能为空", trigger: "blur" }
        ],
        employeeId: [
          { required: true, message: "出库人员id不能为空", trigger: "blur" }
        ],
        createId: [
          { required: true, message: "创建人员id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getWarehouseList();
  },
  methods: {
    // 查询仓库列表
    getWarehouseList() {
      getWarehouseList().then(response => {
        this.warehouseList = response.data;
      })
    },
    /** 查询#出库单列表 */
    getList() {
      this.loading = true;
      listOutbound(this.queryParams).then(response => {
        this.outboundList = response.rows;
        console.log(this.outboundList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.outboundCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path: "/inventory/add/addOutbound", query: {data: '新增'}});
    },
    /** 删除或者撤销按钮操作 */
    handleDelete(row) {
      if (row.status ===1 ) {
        this.$modal.confirm('是否确定撤销该出库订单').then(function () {
          return updateStatus(row);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("撤销成功");
        }).catch(() => {});
      } else {
        const outboundCodes = row.outboundCode || this.ids;
        this.$modal.confirm('是否确认删除出库单编号为"' + outboundCodes + '"的数据项？').then(function() {
          return delOutbound(outboundCodes);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
    /**
     * 确定出库按钮操作
     */
    handleConfirm(row) {
      updateStatus(row).then(response => {
        this.$modal.msgSuccess("出库成功");
        this.getList();
      })
    },
    /**
     * 详情按钮
     */
    handleDetails(row) {
      this.$router.push({path: "/inventory/add/addOutbound", query: {data: '详情',orderCode: row.orderCode,outboundCode: row.outboundCode}});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('inventory/outbound/export', {
        ...this.queryParams
      }, `outbound_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
